package com.platf.dao;

import com.platf.controller.entity.UsersDTO;
import com.platf.pojo.User;
import org.apache.ibatis.annotations.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Mapper
public interface UserDao {
    @Select("select * from t_buser where username = #{username};")
    User findByUserName(String username);

    @Insert("insert into t_buser (username,password,email) values (#{username},#{password},#{email});")
    void register(User registerUser);

    @Insert("insert into t_cuser (username,email) values (#{username},#{email});")
    void registerComplex(User registerUser);

    @Select("select * from t_buser where email = #{email};")
    User findByEmail(String email);

    @Update("update t_buser set password = #{password} where username = #{username}")
    void change(User user);

    @Select("select * from t_buser where id = #{id};")
    User findById(String id);

    @Select("SELECT u.*, COALESCE(r.role, null) AS role " +
            "FROM t_buser u " +
            "LEFT JOIN t_popedom r ON u.username = r.username " +
            "WHERE u.username LIKE #{username} AND u.email LIKE #{email} AND u.telephone LIKE #{phone}")
    List<UsersDTO> selectPage(Integer pageNum, Integer pageSize, String username, String email, String phone);

    @Select("select count(*) from t_buser where username like #{username} and email like #{email} and telephone like #{phone}")
    Integer getTotal(String username,String email,String phone);

    @Update("update t_buser set username = #{username}, email = #{email}, where number = #{number} ")
    void edit(User editUser);

    @Insert("insert into t_buser (username,email,phone,name) values (#{username},#{email},#{phone},#{name});")
    void insert(User editUser);

    @Delete("DELETE tb, tc, tp " +
            "FROM t_buser tb " +
            "INNER JOIN t_cuser tc ON tb.username = tc.username " +
            "INNER JOIN t_popedom tp ON tb.username = tp.username " +
            "WHERE tb.id = #{id};")
    void deleteById(@Param("id") Integer id);

    @Delete("<script>" +
            "DELETE tb, tc, tp " +
            "FROM t_buser tb " +
            "INNER JOIN t_cuser tc ON tb.username = tc.username " +
            "INNER JOIN t_popedom tp ON tb.username = tp.username " +
            "WHERE tb.id IN " +
            "<foreach item='id' collection='ids' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    void deleteBatch(@Param("ids") List<Integer> ids);

}
